package com.tz.farmer.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.config.RedisConfig;
import com.tz.farmer.enums.ErrorCodeEnum;
import com.tz.farmer.mapper.FarmerKnowledgeMapper;
import com.tz.farmer.model.FarmerKnowledge;
import com.tz.farmer.service.WebFarmerKnowledgeService;
import com.tz.farmer.so.FarmerKnowledgeSo;
import com.tz.farmer.util.BeanConvertUtils;
import com.tz.farmer.util.ResultExM;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class WebFarmerKnowledgeServiceImpl extends ServiceImpl<FarmerKnowledgeMapper, FarmerKnowledge> implements WebFarmerKnowledgeService {

     @Resource
    FarmerKnowledgeMapper farmerKnowledgeMapper;

    @Override
    public ResultEx geKnowledgeListByConditions(FarmerKnowledgeSo so) {
        if ( ObjectUtils.isEmpty(so) || so.getFlag()==null || so.getFlag() > 2 || "".equals(so.getFlag())) {
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }
        int total = farmerKnowledgeMapper.countFlag(so.getFlag(),so.getClassify(), so.getKeyword(), so.getStartTime(), so.getEndTime());
        int n = (so.getCurrentPage()-1)*so.getPageSize();
        int m =so.getPageSize();
        List<FarmerKnowledge> list = farmerKnowledgeMapper.selectConditionList(so.getFlag(),so.getClassify(), so.getKeyword(), so.getStartTime(), so.getEndTime(),n,m);
        Map<String, Object> map = new Hashtable<>();
        map.put("total", total);
        map.put("data", list);
        return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED, map);
    }


    @Transactional
    @Override
    public ResultEx updateAddKnowledge(FarmerKnowledgeSo so ,String token) {
        if (ObjectUtils.isEmpty(so) || StringUtils.isEmpty(so.getDoFlag())) {
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }
        if (so.getDoFlag().equals("1")) { //添加
            if ( "".equals(so.getFlag()) || so.getFlag() > 2 ) {
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
            }
            FarmerKnowledge farmerKnowledge = BeanConvertUtils.copyProperties(so, FarmerKnowledge.class);
            Jedis jedis = RedisConfig.getJedis();
//            String s = jedis.get(token);
//            String[] users = s.split("_");
//            String userAccount = users[3];
            String userAccount = "admin";
            farmerKnowledge.setAccount(userAccount);
            farmerKnowledge.setProperty(so.getFlag());
            farmerKnowledge.setTime(LocalDateTime.now());
            boolean save = this.save(farmerKnowledge);
            if(save){
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED);
            }
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_FAIL);

        } else if (so.getDoFlag().equals("2")) { //修改 置顶 下架

            if (so.getId() == null || so.getId() <= 0) {
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
            }
            FarmerKnowledge farmerKnowledge = BeanConvertUtils.copyProperties(so, FarmerKnowledge.class);
            farmerKnowledge.setTime(LocalDateTime.now());
            Jedis jedis = RedisConfig.getJedis();
//            String s = jedis.get(token);
//            String[] users = s.split("_");
//            String userAccount = users[3];
            String userAccount = "admin";
            farmerKnowledge.setAccount(userAccount);
            if(so.getIsTop() != null && so.getIsTop() == 1){
                int i = farmerKnowledgeMapper.selectMaxTop(so.getFlag());
                i=i+1;
                farmerKnowledge.setTop(i);
            }
            boolean update = this.updateById(farmerKnowledge);
            if (update) {
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED);
            }
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_UPDATE_FAIL_ERROR);
        }else if (so.getDoFlag().equals("3")) { //发布

            if (so.getId() == null || so.getId() <= 0) {
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
            }
            FarmerKnowledge farmerKnowledge = farmerKnowledgeMapper.selectOne(new QueryWrapper<FarmerKnowledge>().eq("id", so.getId()));
            if(ObjectUtils.isEmpty(farmerKnowledge)
                    ||"".equals(farmerKnowledge.getClassify())
                    || farmerKnowledge.getClassify() == null
                    || "".equals(farmerKnowledge.getTitle())
                    || farmerKnowledge.getTitle() == null
                    || "".equals(farmerKnowledge.getActiveText())
                    || farmerKnowledge.getActiveText() == null){
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_ISSUE);
            }
            FarmerKnowledge knowledge = BeanConvertUtils.copyProperties(so, FarmerKnowledge.class);
            knowledge.setTime(LocalDateTime.now());
            Jedis jedis = RedisConfig.getJedis();
//            String s = jedis.get(token);
//            String[] users = s.split("_");
//            String userAccount = users[3];
            String userAccount = "admin";
            knowledge.setAccount(userAccount);

            boolean update = this.updateById(knowledge);
            if (update) {
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED);
            }
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_UPDATE_FAIL_ERROR);
        }else {
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }

    }

    @Override
    public ResultEx getOneDetails(FarmerKnowledgeSo so) {
        if("".equals(so.getId()) || so.getId() == null){
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }
        FarmerKnowledge farmerKnowledge = farmerKnowledgeMapper.selectById(so.getId());
        if(ObjectUtils.isEmpty(farmerKnowledge)){
            return  ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_NO_DATA);
        }
        return  ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED,farmerKnowledge);
    }

    @Override
    public ResultEx deleteOne(FarmerKnowledgeSo so) {
        if("".equals(so.getId()) || so.getId() == null){
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }
        int id = farmerKnowledgeMapper.delete(new QueryWrapper<FarmerKnowledge>().eq("id", so.getId()));
        if(id > 0){
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED);
        }

        return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_DELETE_FAIL_ERROR);
    }






}
